<!--
 * @Author: Haochen
 * @Date: 2021-12-08 20:58:52
 * @LastEditTime: 2021-12-09 19:48:44
 * @FilePath: \students-system\src\views\teachers\DeleteTeacher.vue
-->
<template>
  <div>
    <!-- <bread-crumb /> -->
    <h4>删除教师</h4>
    <el-select v-model="teachersId" placeholder="请选择教师">
      <el-option
        v-for="teacher in allTeachers"
        :key="teacher._id"
        :label="teacher.name"
        :value="teacher._id"
      >
      </el-option>
    </el-select>
    &nbsp;
    <el-button type="primary" @click="deleteTeacher()">确认删除</el-button>

    <h4>教师列表</h4>
    <br />
    <h5>搜索教师</h5>
    <div class="list-row">
      <el-input
        id="input-search"
        type="text"
        v-model.lazy="searchData.value"
        style="width: 200px"
      />
      &nbsp;
      <el-button type="info" @click="confirmSearch">确认搜索</el-button>
    </div>

    <el-table :data="teachers" border style="width: 100%">
      <el-table-column prop="name" label="教师名称" align="center">
      </el-table-column>

      <el-table-column label="教师操作" width="180" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row._id)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="deleteTeacher(scope.row._id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <Pages
      :total="total"
      :currentPage="pageData.currentPage"
      :pageSize="pageData.pageSize"
      @prevPage="prevPage"
      @nextPage="nextPage"
    />
  </div>
</template>

<script>
import bus from "@/views/students/bus.js";
import BreadCrumb from "../../components/common/BreadCrumb.vue";
import { createNamespacedHelpers } from "vuex";
import Pages from "../../components/common/Pages.vue";
const { mapActions: teachersActions, mapState: teachersState } =
  createNamespacedHelpers("teachers");
export default {
  data() {
    return {
      teachersId: undefined,
      teachers: [],
      allTeachers: [],
      total: 0,
      pages: 0,
      pageData: { currentPage: 1, pageSize: 3 },
      searchData: { value: "" },
    };
  },
  components: {
    BreadCrumb,
    Pages,
  },
  created() {
    this.getAllTeachers();
    bus.$on("sendPageSize", (size) => {
      this.pageData.pageSize = size;
      this.pageData.currentPage = 1;
    });
    this.getTeachers();
  },
  methods: {
    async getAllTeachers() {
      const { data: res } = await this.$api.teachers.getTeachers();
      this.allTeachers = res.data.rows;
    },
    async deleteTeacher() {
      const { data: res } = await this.$api.teachers.deleteTeacher({
        _id: this.teachersId,
      });

      if (res.code) {
        alert(res.message);
        this.teachersId = undefined;
        this.getAllTeachers();
      } else {
        alert(res.message);
      }
    },
    async getTeachers() {
      const { data: res } = await this.$api.teachers.getTeachers({
        ...this.pageData,
        ...this.searchData,
      });

      this.total = res.data.total;
      this.pages = res.data.pages;
      this.teachers = res.data.rows;
    },

    confirmSearch() {
      this.getTeachers();
      this.pageData.currentPage = 1;
    },
    // * 下一页功能
    nextPage() {
      if (this.pageData.currentPage < this.pages) {
        this.pageData.currentPage++;
      }
    },

    // * 上一页功能
    prevPage() {
      if (this.pageData.currentPage > 1) {
        this.pageData.currentPage--;
      }
    },
    // * 首页功能
    firstPage() {
      this.pageData.currentPage = 1;
    },
    // * 尾页功能
    lastPage() {
      this.pageData.currentPage = this.pages;
    },
  },
  watch: {
    pageData: {
      deep: true,
      handler() {
        this.getTeachers();
      },
    },
  },
};
</script>

<style></style>
